import { AppComponentBase } from '@shared/common/app-component-base';
import { Component, OnInit, Injector } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { ServiceProxy, DictionaryDTO } from '@shared/service-proxy/service-proxies';
import { NzMessageService, isTemplateRef } from 'ng-zorro-antd';

@Component({
  selector: 'app-notice-rule',
  templateUrl: './notice-rule.component.html',
  styleUrls: ['./notice-rule.component.less']
})
export class NoticeRuleComponent extends AppComponentBase implements OnInit {
  dataSet = [];
  constructor(injector: Injector, private fb: FormBuilder, private _service: ServiceProxy) {
    super(injector)
  }

  ngOnInit() {
    this.getNoticeRule();
  }


  getNoticeRule() {
    this._service.getDictionaryByDictionaryTypeUsingGET("notice_rule").subscribe(result => {
      this.dataSet = result.result.fieldList;

      this.dataSet.forEach(element => {
        element.enable = element.itemValue == "true";
      });
    })
  }

  handleUsing(data: DictionaryDTO) {
    const length = this.dataSet.filter(m => m.itemValue == 'false').length;
    if (data.itemValue == "true") {
      data.itemValue = "false";
    }
    else if (data.itemValue == "false") {
      data.itemValue = "true";
    }

    if (length == 4 && data.itemValue == "false") {
      this.message.info(this.l('can_not_close_together'));
      this.getNoticeRule();
    } else {
      this._service.updateDictionaryUsingPUT(data.id, data).subscribe(result => {
        if (result.success) {
          this.message.success(this.l("save_success"))
        }
        else
          this.message.info(this.l(result.error.message));
      });
    }


  }
}
